package org.tgcms.core.dal.appengine.dao;

import org.tgcms.core.dal.appengine.domain.AppengineTemplate;
import org.tgcms.core.dal.appengine.domain.AppengineWebsite;
import org.tgcms.core.dal.dao.DaoException;
import org.tgcms.core.dal.dao.TemplateDao;
import org.tgcms.core.dal.dto.Template;
import org.tgcms.core.dal.dto.Website;

import com.google.appengine.api.datastore.Key;

public class AppengineTemplateDao extends
		AppengineEntityDao<AppengineTemplate, Template, Website> implements
		TemplateDao {

	public AppengineTemplateDao() {
		super(AppengineTemplate.class, Template.class);
	}

	@Override
	protected AppengineTemplate asAppengineEntity(Template template)
			throws DaoException {
		if (template instanceof AppengineTemplate) {
			return (AppengineTemplate) template;
		}
		return null;// new AppengineTemplate(template);
	}

	@Override
	public Template create(Website website) throws DaoException {
		AppengineWebsite appengineWebsite = checkWebsite(website);
		Key key = datastore
				.allocateIds(appengineWebsite.getKey(),
						AppengineTemplate.class.getSimpleName(), 1).iterator()
				.next();
		return new AppengineTemplate(key);
	}

}
